﻿using System.ComponentModel;
using UtfprLibrary.ComponentModel.Security;
using UtfprLibrary.Helpers;
using UtfprLibrary.Exceptions;
using System;

namespace UtfprLibrary.ComponentModel {
    public class UtfprControlBase : IUtfprDesignTimeService, IUtfprRestrictionService {
        private IUtfprControl _utfprControl;

        public UtfprControlBase(IUtfprControl utfprControl) {
            this._utfprControl = utfprControl;
        }

        #region IUtfprDesignTimeService

        public bool IsInDesignMode {
            get {
                try {
                    return DesignTimeHelper.IsInDesignMode;
                } catch (Exception ex) {
                    throw new UtfprComponentModelException(Properties.Resources.ComponentModel_UtfprControlBase_ExMsg_IsInDesignMode, ex);
                }
            }
        }

        #endregion

        #region IUtfprRestrictionService

        [Category("UTFPR Security"), Description("True to apply security, otherwise false.")]
        public bool IsSecureRestriction { get; set; }

        private bool _isFunction = true;

        [Category("UTFPR Security"), Description("True if it is a Function, otherwise false.")]
        public bool IsFunctionRestriction {
            get { return _isFunction; }
            set {
                _isFunction = value;
                if (IsInDesignMode) {
                    FunctionOrOperationRestriction = null;
                }
            }
        }

        public void SetVisibleRestriction(bool isVisible) {
            try {
                this._utfprControl.UtfprControlVisible = isVisible;
            } catch (Exception ex) {
                throw new UtfprComponentModelException("Could not set the 'UtfprControlVisible' property.", ex);
            }
        }

        public void SetEnabledRestriction(bool isEnabled) {
            this._utfprControl.UtfprControlEnabled = isEnabled;
        }

        public IUtfprKeyName FunctionOrOperationRestriction {
            get {
                try {
                    return this._utfprControl.FunctionOrOperationRestriction;
                } catch (Exception ex) {
                    throw new UtfprComponentModelException("Could not get the 'FunctionOrOperationRestriction' property.", ex);
                }
            }
            set {
                try {
                    this._utfprControl.FunctionOrOperationRestriction = value;
                } catch (Exception ex) {
                    throw new UtfprComponentModelException("Could not set the 'FunctionOrOperationRestriction' property.", ex);
                }
            }
        }
        
        #endregion
    }
}
